<extend name="desktop_default/template/base_index2" />

<block name="area_header">
    <link href="__CSS__/cart.css?v=__APP_VERSION__" rel="stylesheet" type="text/css" />
	<style type="text/css">

    </style>
</block>

<block name="area_body">
    <include file="desktop_default/width/right" />

    <header class="header">
        <include file="desktop_default/width/top" />
        {:W('Partials/nav_group')}
    </header>



    <div class="shoppingcart am-cf">
        <form class="cart_form" method="post">
        <empty name="cart">
            <div class="shop-container am-text-center am-padding-lg">
                <div class="am-text-center am-padding-sm">您的购物车还是空的，赶紧行动吧！</div>
                <div class=" am-padding-sm">
                <i style="font-size: 5rem;color:#dedede;" class="am-icon-shopping-cart am-icon-lg am-text-info"></i>
                </div>
                <div class=" am-padding-sm">
                    <a href="{:U('Shop/Index/index')}" class="am-btn am-btn-primary">到处逛逛</a>
                </div>
            </div>

            <else/>


                <div class="am-padding-sm am-cf cart-main shop-container">

                    <include file="desktop_default/partial/buy_step" step="1" />
                    <div class="am-cf cart-table-th">
                            <div class="th th-chk">
                                <div class="select-all ">
                                    <input class="js_select_all" type="checkbox" >
                               &nbsp;&nbsp;全选
                                </div>
                            </div>

                            <div class="th th-item">
                                <div class="td-inner">商品信息</div>
                            </div>

                            <div class="th th-info">
                                <div class="td-inner">&nbsp;</div>
                            </div>

                            <div class="th th-price">
                                <div class="td-inner">单价（元）</div>
                            </div>

                            <div class="th th-amount">
                                <div class="td-inner">数量</div>
                            </div>

                            <div class="th th-sum">
                                <div class="td-inner">金额（元）</div>
                            </div>

                            <div class="th th-op">
                                <div class="td-inner">操作</div>
                            </div>

                    </div>
                    <div class="item-list am-cf">
                    <volist name="cart" id="vo">
                        <div class="cart-item am-fl  am-padding-sm am-margin-top-sm">
                            <div class=" am-fl td td-chk">
                                <input type="checkbox" value="{$vo['id']}" name="cart_id[]" class="am-checkbox js_select_item"/>
                            </div>

                            <div class="am-fl td td-item am-padding-sm">
                                <img class="am-fl icon-url" src="{:getImageUrl($vo['icon_url'],80)}" alt="图片" STYLE="width: 80px;height:80px;" />

                                <div class="item-info">
                                    <div class="item-basic-info am-padding-left-sm">
                                       <a  target="_blank"  href="{:U('Shop/Product/detail',array('id'=>$vo['p_id']))}">{$vo.name}</a>
                                    </div>
                                </div>
                            </div>
                            <div class="td td-info js_item_price am-fl am-padding-sm">
                                {$vo.sku_desc}
                            </div>
                            <div class="td td-price js_item_price am-fl am-padding-sm">
                                <div class="price-content">
                                    <div class="price-line">
                                        <span class="price-original">{:round($vo['ori_price'],2)}</span>
                                    </div>
                                    <div class="price-line">
                                    <span class="price-now" tabindex="0">{:round($vo['price'],2)}</span>
                                    </div>
                                </div>

                            </div>
                            <div class="td td-amount js_item_count am-fl am-padding-sm">
                                {$vo.count}
                            </div>
                            <div class="js_item_total_price td td-sum am-fl am-padding-sm">
                                {:round($vo['count'] * $vo['price'],2)}
                            </div>
                            <div class="td td-op am-fl">
                                <a class="ajax-get " href="{:U('Shop/ShoppingCart/delItem',array('id'=>$vo['id']))}">删除</a>
                            </div>
                        </div>
                    </volist>
                </div>

                    <div class="toolbar am-cf">
                        <a href="{:U('Shop/Orders/checkout')}" data-am-loading="{spinner: 'circle-o-notch', loadingText: '请求中...'}" data-no-alert="1" target-form="cart_form" class="ajax-post am-btn am-btn-danger am-fr btn-submit">结算</a>
                        <div class="am-fr am-margin-right-sm">合计（不含运费）:<span class="am-text-danger am-padding-right-sm">¥</span><span class="js_total_price  am-text-lg am-text-danger"></span><i class="am-icon-spinner am-icon-pulse"></i></div>
                        <div class="am-fr am-margin-right-sm">已选商品&nbsp;<span class="js_total_count am-h1"> 0</span>&nbsp;件</div>
                    </div>

                </div>
        </empty>
        </form>
    </div>


    <include file="desktop_default/width/floor" />

</block>

<block name="area_footer">

<script type="text/javascript">

    //计算价格
    function calculatePrice(){
        var $priceNowList = $(".price-now");
        var $priceOriginList = $(".price-original");
        var $itemList = $(".js_item_count");
        var $itemPriceList = $(".js_item_total_price");
        var totalPrice = 0;
        var totalCnt = 0;
        $(".js_select_item").each(function(index,item){
            if($(item).prop('checked')){
                var itemPrice = parseFloat($($itemPriceList[index]).text());
                var cnt = parseInt($($itemList[index]).text());
//                var price = $($priceNowList[index]).text();

//                console.log(cnt,itemPrice);
//                console.log(cnt,price);
                totalPrice = totalPrice + itemPrice ;
                totalCnt++;

            }
        });

        if(totalCnt > 0){

            $(".js_total_price").text(totalPrice);
            $(".js_total_count").text(totalCnt);
            $(".toolbar .am-icon-spinner").addClass("am-hide");

        }else{
            $(".js_total_price").text(0);
            $(".js_total_count").text(0);

        }
    }

    $(function(){
        console.log("init");
        $(".js_select_all").click(function(){

            console.log("click");
            var checked = $(".js_select_all").prop("checked");
            console.log(checked);
            $(".js_select_item").each(function(index,item){
                $(item).prop("checked",checked);
            });
            calculatePrice();
        });
        $(".js_select_item").click(function(){
            calculatePrice();
        })


        var $totalPrice = $(".js_total_price");

        $(".js_select_all").click();

    });

</script>

</block>